package com.hopes.bizz.poll.activemq.consumer;

import com.hopes.bizz.poll.activemq.config.ActiveMqStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/5/25 13:57
 * @Description:
 */
@Component
@DependsOn("activeMqStore")
@Slf4j
public class ActiveMqQueueConsumer {
    @Autowired
    private ActiveMqStore activeMqStore;

    public MessageConsumer addConsumer(String queue, MessageListener messageListener) {
        MessageConsumer consumer;
        try {
            Session session = activeMqStore.getSession();

            consumer = session.createConsumer(session.createQueue(queue));
            consumer.setMessageListener(messageListener);
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
        return consumer;
    }
}
